var globalRooms = new Array();

function Room(id){
	if(isNumeric(id)){
		this.id = id;
		this.group = null;
	} 
	else{
		this.id = null;
		this.group = id;
	}

	this.list = new Array();

	if(isNumeric(id)) global.createObject('room',this);
}

Room.prototype.getKey = function(){
	return 'room_'+this.id;
}

Room.prototype.set = function(group){
	this.group = group;
}

Room.prototype.addUserList = function(name,list){
	list = sort(list, 'name');

	this.list[name] = list;
}

Room.prototype.updateUserList = function(user,oldListName,newListName,clone){
	if(clone !== false) clone = true;

	if(isString(user)){
		if(isNumeric(user)) user = 'user_'+user;

		user = global.getObject('user',user);
	}

	oldList = this.list[oldListName];
	newList = this.list[newListName];
	//if(newList == undefined || newList == 'undefined') alert(newListName)
	var current;

	for(var i=0, iMax=oldList.length; i<iMax; i++){
		current=oldList[i];

		if(current.name == user.name){
			oldList.splice(i,1); //remove from oldList
			newList.push(current); //add to new List
			break; //leave the loop
		}
	}

	newList = sort(newList,'name'); //sort the new list with the new user
	this.updateUserListElement(newList,global.getElement(newListName),clone);
}

Room.prototype.updateUserListElement = function(array,list,clone){
	if(isString(list)) list = this.list[list];

	array = sort(array,'name');
	var currentUser;
	var fragment = document.createDocumentFragment();
	
	var userEl;	
	for(var i=0, iMax=array.length;i<iMax;i++){
		currentUser = array[i]['obj'];
		userEl = currentUser.createRoomMemberItem(clone);
		fragment.appendChild(userEl);
	}
	
	clearElement(list);
	list.appendChild(fragment);
}

Room.prototype.createOpenedRoomItem = function(obj){
	var openedRoom = document.createElement('DIV');
	openedRoom.className = 'openedRoom item';

	var name = document.createElement('P');
	name.appendChild(document.createTextNode(this.group.name));

	setEvent(openedRoom, 'click', enterRoom);

	setData(openedRoom, 'key', this.getKey());
	openedRoom.appendChild(name);

	return openedRoom;
}

Room.prototype.userPerformAccess = function(userId, type){
	var user = global.getObject('user', 'user_'+userId);

	var access = document.createElement('DIV');
	access.className = 'access';

	var accessTypeClassName, accessTypeText;
	if(type){
		accessTypeText = ' entrou na sala';
		accessTypeClassName = ' login';	
	} 
	else{
		accessTypeText = ' saiu da sala';
		accessTypeClassName = ' logout';	
	} 

	access.className += accessTypeClassName;

	var icon = document.createElement('DIV');
	icon.className = 'icon';

	var text = document.createElement('P');
	text.appendChild(document.createTextNode(user.name+accessTypeText));

	access.appendChild(icon);
	access.appendChild(text);

	var messages = global.getElement('messages');
	messages.appendChild(access);

	user.changeStatus(type);
}

function enterRoom(event){
	var item = findParentWithClass(event.target, 'item', true);
	var roomId = getData(item,'room');
	
	var com = new Communicator();
	com.addParameter('type','user');
	com.addParameter('task','enterRoom');
	com.addParameter('data',JSON.stringify({room: roomId}));

	com.sendRequest(enterRoomHandler);
}

function enterRoomHandler(response){ 
	goToRoom(eval(response))
}

function goToRoom(room,errorMessage){
	if(!errorMessage) errorMessage = 'Houve um problema. Tente novamente em alguns minutos ou entre em contato com o administrador do sistema.';
	if(isNumeric(room)) loadUrl(globalServerURL+'room.php?id='+room);
	else{
		if(!room || room == '') alert(errorMessage);
	}
}

function leaveRoom(event){
	if(event){
		var item = findParentWithClass(event.target,'item',true);
		var session = getData(item,'session');
		var room = getData(item,'room');
	}

	var obj = {};
	if(session){
		obj.session = session;	
		obj.room = room;
	} 
	else{
		if(!room) room = global.getObject('room').id;

		obj.room = room;
	} 

	var com = new Communicator();
	com.addParameter('type','user');
	com.addParameter('task','leaveRoom');
	com.addParameter('data',JSON.stringify(obj));
	com.sendRequest(backToIndex);
}

function closeRoom(event){
	if(event){
		var item = findParentWithClass(event.target, 'item', true);
		var room = getData(item,'room');
	}

	if(!room) room = global.getObject('room').id;

	var com = new Communicator();
	com.addParameter('type','user');
	com.addParameter('task','closeRoom');
	com.addParameter('data',JSON.stringify({room: room}));
	com.sendRequest(backToIndex);
}

function backToIndex(response){
	loadUrl(globalServerURL+'index.php');
}

function receiveAccess(data){
	var userId = data[1];
	var time = data[2];
	var type = data[0];

	var room = global.getObject('room');
	room.userPerformAccess(userId, type);
}

function openRoom(event){
	var item = findParentWithClass(event.target, 'item', true);
	var groupId = getData(item,'group');
	
	var com = new Communicator();
	com.addParameter('type','user');
	com.addParameter('task','openRoom');
	com.addParameter('data',JSON.stringify({group: groupId}));
	com.sendRequest(openRoomHandler);
}

function openRoomHandler(response){
	goToRoom(eval(response))
}